// pages/mine/mine.js
import ajax from '../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    pageStatus: 0, // 页面状态 0 普通首页, 1 园区首页
    gardenRoleItem: null,
    roleArr: ["yuanqunguanli", "fangyuanguanli", "hetongguanli", "yuanggongguanli", "haibaozhizuo", "qiyebaoxiu", "baobeiguanli", 'gardenSuperAdmin', 'gardenAdmin'],
    isSwitch: false,
    showLoginPicker: false,
    succeedMode: false,
  },
  loginSuccess() {
    this.loginCancel()
    this.getUserInfo()
  },
  loginCancel() {
    this.setData({ showLoginPicker: false })
  },
  isModeEvent() {
    this.setData({ showLoginPicker: true })
  },
  serviceEvent() {

  },
  hairOpenEvent() {
    ajax.showToast('开发中...')
  },
  /**
   * 页面状态切换
   */
  pageRolesEvent(event) {
    const status = event.currentTarget.dataset.status - 0
    const { userInfo, roleArr } = this.data
    for (let index = 0; index < roleArr.length; index++) {
      const element = roleArr[index];
      if (userInfo.role.some(item => item == element)) (wx.setStorageSync('pageRoles', status), this.setData({ pageStatus: status }))
    }
    this.setData({ succeedMode: true })
    setTimeout(() => this.setData({ succeedMode: false }), 2000)
  },
  async getUserInfo() {
    const roleArr = this.data.roleArr
    const userInfo = await ajax.getUserInfo({ isLogin: true })
    if (!userInfo) return this.setData({ userInfo: null })
    userInfo.isMaster = userInfo.role.some(item => item == 'master')
    userInfo.isAgent = userInfo.role.some(item => item == 'agent')
    for (let index = 0; index < roleArr.length; index++) {
      const element = roleArr[index];
      if (userInfo.role.some(item => item == element)) (this.setData({ isSwitch: true }))
    }
    this.setData({ userInfo })
  },
  serviceEvent() {
    ajax.makePhoneCall(getApp().globalData.serviceTel)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
    console.log(wx.getStorageSync('gardenRoleItem'))
    this.setData({
      gardenRoleItem: wx.getStorageSync('gardenRoleItem') || null,
      pageStatus: wx.getStorageSync('pageRoles') || 0
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})